<!-- 订单筛选 -->
<template>
  <view class="order-list-page">
    <!-- 订单状态筛选 -->
    <view class="order-tabs">
      <view 
        class="tab-item" 
        :class="{active: activeTab === 'all'}"
        @click="switchTab('all')"
      >
        全部
      </view>
      <view 
        class="tab-item" 
        :class="{active: activeTab === 'waitPay'}"
        @click="switchTab('waitPay')"
      >
        待支付
      </view>
      <view 
        class="tab-item" 
        :class="{active: activeTab === 'waitUse'}"
        @click="switchTab('waitUse')"
      >
        待使用
      </view>
      <view 
        class="tab-item" 
        :class="{active: activeTab === 'waitComment'}"
        @click="switchTab('waitComment')"
      >
        待评价
      </view>
    </view>

    <!-- 订单列表 -->
    <scroll-view 
      class="order-container" 
      scroll-y 
      @scrolltolower="loadMore"
    >
      <!-- 待评价订单 -->
      <view 
        class="order-card" 
        v-for="order in filteredOrders" 
        :key="order.id"
      >
        <view class="order-header">
          <text class="order-no">订单编号：{{order.orderNo}}</text>
          <text class="order-status">{{getStatusText(order.status)}}</text>
        </view>
        
        <view class="order-content">
          <text class="order-item">服务项目：{{order.serviceName}}</text>
          <text class="order-item">服务门店：{{order.shopName}}</text>
          <text class="order-item">下单时间：{{order.createTime}}</text>
          <text class="order-item">支付：{{order.amount}}元</text>
        </view>
        
        <view class="order-footer">
          <button 
            v-if="order.status === 'waitComment'" 
            class="action-btn primary"
            @click="goToComment(order.id)"
          >
            立即评价
          </button>
          <button class="action-btn" @click="reorder(order.id)">
            再来一单
          </button>
        </view>
      </view>
      
      <view class="load-more" v-if="loading">
        <text>加载中...</text>
      </view>
      <view class="no-more" v-if="!hasMore">
        <text>没有更多了</text>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

// 订单状态常量
const ORDER_STATUS = {
  waitPay: 'waitPay',      // 待支付
  waitUse: 'waitUse',      // 待使用
  waitComment: 'waitComment', // 待评价
  completed: 'completed',   // 已完成
  cancelled: 'cancelled',   // 已取消
  refunded: 'refunded'      // 已退款
}

// 当前激活的标签
const activeTab = ref('all')
// 订单列表
const orders = ref([])
// 是否正在加载
const loading = ref(false)
// 是否还有更多数据
const hasMore = ref(true)
// 当前页码
const currentPage = ref(1)

// 从路由参数获取初始类型
onLoad((options) => {
  if (options.type) {
    activeTab.value = options.type
  }
  fetchOrders()
})

// 获取状态文本
const getStatusText = (status) => {
  const texts = {
    [ORDER_STATUS.waitPay]: '待支付',
    [ORDER_STATUS.waitUse]: '待使用',
    [ORDER_STATUS.waitComment]: '待评价',
    [ORDER_STATUS.completed]: '已完成',
    [ORDER_STATUS.cancelled]: '已取消',
    [ORDER_STATUS.refunded]: '已退款'
  }
  return texts[status] || status
}

// 筛选后的订单列表
const filteredOrders = computed(() => {
  if (activeTab.value === 'all') {
    return orders.value
  }
  return orders.value.filter(order => order.status === activeTab.value)
})

// 切换标签
const switchTab = (tab) => {
  activeTab.value = tab
  refreshOrders()
}

// 刷新订单列表
const refreshOrders = () => {
  currentPage.value = 1
  hasMore.value = true
  orders.value = []
  fetchOrders()
}

// 获取订单列表
const fetchOrders = async () => {
  if (loading.value || !hasMore.value) return
  
  loading.value = true
  
  try {
    // 模拟API请求
    const mockData = getMockOrders(currentPage.value, activeTab.value)
    
    if (currentPage.value === 1) {
      orders.value = mockData
    } else {
      orders.value = [...orders.value, ...mockData]
    }
    
    // 模拟没有更多数据的情况
    if (currentPage.value >= 3) {
      hasMore.value = false
    } else {
      currentPage.value++
    }
  } catch (error) {
    console.error('获取订单失败:', error)
    uni.showToast({
      title: '获取订单失败',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

// 加载更多
const loadMore = () => {
  fetchOrders()
}

// 去评价
const goToComment = (orderId) => {
  uni.navigateTo({
    url: `/pages/order/Evaluation/Evaluation?orderId=${orderId}`
  })
}

// 再来一单
const reorder = (orderId) => {
  uni.showLoading({ title: '处理中...' })
  // 模拟API请求
  setTimeout(() => {
    uni.hideLoading()
    uni.showToast({
      title: '已加入服务预约',
      icon: 'success'
    })
  }, 1000)
}

// 模拟数据生成
const getMockOrders = (page, type) => {
  const baseOrders = [
    {
      id: `${page}001`,
      orderNo: '24255252522',
      serviceName: '车辆保养',
      shopName: '大宝车辆维修店',
      createTime: '2022-12-20 12:09',
      amount: '200',
      status: ORDER_STATUS.waitComment
    },
    {
      id: `${page}002`,
      orderNo: '24255252523',
      serviceName: '轮胎更换',
      shopName: '小宝车辆维修店',
      createTime: '2022-12-21 10:30',
      amount: '500',
      status: ORDER_STATUS.completed
    },
    {
      id: `${page}003`,
      orderNo: '24255252524',
      serviceName: '空调清洗',
      shopName: '大宝车辆维修店',
      createTime: '2022-12-22 14:15',
      amount: '150',
      status: ORDER_STATUS.cancelled
    },
    {
      id: `${page}004`,
      orderNo: '24255252525',
      serviceName: '全车检测',
      shopName: '小宝车辆维修店',
      createTime: '2022-12-23 09:45',
      amount: '100',
      status: ORDER_STATUS.refunded
    }
  ]
  
  // 根据类型筛选模拟数据
  if (type === 'all') {
    return baseOrders
  }
  
  return baseOrders.filter(order => order.status === type)
}
</script>

<style lang="scss">
.order-list-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
}

.order-tabs {
  display: flex;
  background-color: #fff;
  padding: 20rpx 0;
  position: sticky;
  top: 0;
  z-index: 10;
  
  .tab-item {
    flex: 1;
    text-align: center;
    font-size: 28rpx;
    color: #666;
    padding: 15rpx 0;
    
    &.active {
      color: #2196f3;
      font-weight: bold;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 80rpx;
        height: 4rpx;
        background-color: #2196f3;
        border-radius: 2rpx;
      }
    }
  }
}

.order-container {
  flex: 1;
  padding: 20rpx;
  box-sizing: border-box;
}

.order-card {
  background-color: #fff;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  padding: 30rpx;
  
  .order-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    padding-bottom: 20rpx;
    border-bottom: 1rpx solid #eee;
    
    .order-no {
      font-size: 26rpx;
      color: #666;
    }
    
    .order-status {
      font-size: 28rpx;
      color: #ff9900;
      
      &.completed {
        color: #07c160;
      }
      
      &.cancelled, &.refunded {
        color: #999;
      }
    }
  }
  
  .order-content {
    .order-item {
      display: block;
      font-size: 26rpx;
      color: #666;
      margin-bottom: 15rpx;
      
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  
  .order-footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 30rpx;
    padding-top: 20rpx;
    border-top: 1rpx solid #eee;
    
    .action-btn {
      margin-left: 20rpx;
      padding: 0 30rpx;
      height: 60rpx;
      line-height: 60rpx;
      font-size: 26rpx;
      color: #666;
      background-color: #fff;
      border: 1rpx solid #ddd;
      border-radius: 30rpx;
      
      &.primary {
        color: #fff;
        background-color: #2196f3;
        border-color: #2196f3;
      }
    }
  }
}

.load-more, .no-more {
  text-align: center;
  padding: 20rpx 0;
  font-size: 26rpx;
  color: #999;
}
</style>